<?php

class Admin extends Controller {
	var $auth;
	function Admin() {
		parent::Controller();	
		$this->load->library('pear_auth', array('table'=>'users'));
		$this->auth = $this->pear_auth->auth_info();
		
	}
	
	function index() {
		$data["layout"] = "admin/index.php";
		$data["auth"] = $this->auth;
		
		$query = $this->db->query("SELECT COUNT(*) AS users FROM users WHERE acc_type='user'");
		$data['users'] = $query->result_array();
		$query = $this->db->query("SELECT COUNT(*) AS doctors FROM users WHERE acc_type='doctor'");
		$data['doctors'] = $query->result_array();
		$query = $this->db->query("SELECT COUNT(*) AS admins FROM users WHERE acc_type='admin'");
		$data['admins'] = $query->result_array();
		$query = $this->db->query("SELECT COUNT(*) AS comments FROM rating_comments");
		$data['rating_comments'] = $query->result_array();		
		
		
		$this->load->view('admin', $data);
	}
	function users_view() {
		$data["layout"] = "admin/users.php";
		$data["auth"] = $this->auth;
		$this->load->library("rapyd");
		$this->rapyd->load("dataedit");

		$edit = new DataEdit("Users", "users");
		$edit->back_uri = "admin/users";
		$edit->back_save = true;
		$edit->back_delete = true;
		$edit->back_cancel_save = true;
		$edit->back_cancel_delete = true;

		$edit->username = new inputField("Username", "username");		
		$edit->new_password = new inputField("New password<br>(leave blank if dont want change)", "new_password");		

		//$edit->acc_type = new radiogroupField("Account type", "acc_type", array("root"=>"Root","admin"=>"Admin", "doctor"=>"Doctor", "user"=>"User")); //enum('root', 'admin', 'supervisor', 'user')
		$edit->acc_type = new radiogroupField("Account type", "acc_type", array("admin"=>"Admin", "doctor"=>"Doctor", "user"=>"User", 'guest' => 'Guest'));
		$edit->acc_type->insertValue = "user";

		$edit->email = new inputField("E-mail", "email");
		
		$edit->fname = new inputField("First name", "fname");
		$edit->mname = new inputField("Middle name", "mname");
		$edit->lname = new inputField("Last name", "lname");

		$edit->active = new radiogroupField("Status", "active", array("1"=>"Enabled","0"=>"Disabled"));
		$edit->active->insertValue = "1";		

		$edit->sex = new radiogroupField("Sex", "sex", array("m"=>"Male","f"=>"Female", "u"=>"Undefined"));
		$edit->sex->insertValue = "u";
		
		$edit->birth = new dateField("Birthday", "birth","us");
		$edit->fname->group = "Personal info";
		$edit->mname->group = "Personal info";
		$edit->lname->group = "Personal info";
		$edit->sex->group = "Personal info";
		$edit->birth->group = "Personal info";
				
		$edit->address = new inputField("Address", "address");
		$edit->address->group = "Location";
		$edit->city = new inputField("City", "city");
		$edit->city->group = "Location";
		$edit->state = new inputField("State", "state");
		$edit->state->group = "Location";
		$edit->zip = new inputField("Zip", "zip");
		$edit->zip->group = "Location";
		
		$edit->last_login = new dateField("Last login", "changed");
		$edit->registered = new dateField("Created login", "added");
		

		$edit->buttons("modify", "save", "undo", "delete");
		$edit->build();


		$data["crud"] = $edit->output;
		$data["rapyd_head"] = $this->rapyd->get_head();

		if ($edit->on_success()){
			if ($_POST['new_password'] != '')
				$this->db->query("UPDATE users SET password=MD5('{$_POST['new_password']}') WHERE username = '{$_POST['username']}'");
		}
		$this->load->view('admin', $data);
	}
	function users() {
		$data["layout"] = "admin/users.php";
		$data["auth"] = $this->auth;
		$this->load->library("rapyd");
		$this->rapyd->load("datafilter","datagrid");
		$this->rapyd->uri->keep_persistence();

		$filter = new DataFilter("Users Filter");
		$filter->db->select("users.*");
		$filter->db->from("users");
		$filter->username = new inputField("Username", "username");
		$filter->fname = new inputField("First Name", "fname");
		$filter->lname = new inputField("Last Name", "lname");
		$filter->email = new inputField("E-mail", "email");
		$filter->buttons("reset","search");
		$filter->build();

		$uri = "admin/users/show/<#id#>";

		$grid = new DataGrid("Users List");
		$grid->order_by("username","desc");
		$grid->per_page = 10;
		$grid->column("Id","id");
		$grid->column_orderby("Username","username","username");
		$grid->column_orderby("Account type","acc_type","acc_type");
		$grid->column_orderby("E-mail","email","email");
		$grid->column_orderby("Last login","changed","changed");

		$link_view = anchor("admin/users_view/0/show/<#id#>","View");
		$grid->column("Actions", "$link_view");

		$grid->add("admin/users_view/create");
		$grid->build();

		$data["crud"] = $filter->output . $grid->output;
		$data["rapyd_head"] = $this->rapyd->get_head();
		$this->load->view('admin', $data);
	}
	
	function cms(){
		$this->load->library("rapyd");
		$data["layout"] = "admin/cms.php";
		$data["auth"] = $this->auth;
		$this->rapyd->load("datagrid", "dataedit");
		$grid = new DataGrid("Content Management", "cms_content");
		$grid->per_page = 10;
		$grid->use_function("strip_tags", "substr");
		$grid->column("Title","title", 'width="150"');
		$grid->column("Content","<strip_tags><substr><#content#>|0|350</substr></strip_tags>...", 'width="500"');
		$baseuri = "admin/cms/".$this->uri->segment(5);
		$link_edit = anchor("admin/cms/0/modify/<#id#>","Edit");
		$link_delete = anchor("admin/cms/0/delete/<#id#>","Delete");

		//$grid->column("actions", "$link_edit | $link_delete", 'width="90"');
		$grid->column("actions", "$link_edit", 'width="50"');
		$grid->build();

		//edit
		$edit = new DataEdit("Edit content", "cms_content");
		$edit->back_uri = $baseuri;
		$edit->back_save = true;
		$edit->back_delete = true;
		$edit->back_cancel_save = true;
		$edit->back_cancel_delete = true;

		$edit->title = new inputField("Title", "title");
		$edit->content = new editorField("Content", "content");
		$edit->content->cols = 60;
		$edit->content->rows = 15;

		$edit->buttons("modify", "save", "undo", "delete");
		$edit->build();

		$data["crud"] = $grid->output . $edit->output;

		$data["rapyd_head"] = $this->rapyd->get_head();
		$this->load->view('admin', $data);
	}

	function rating () {
		$this->load->library("rapyd");
		$data["layout"] = "admin/rating.php";
		$data["auth"] = $this->auth;
		$this->rapyd->load("datagrid", "dataedit");
		$grid = new DataGrid("Rating config", "rating_items");
		$grid->per_page = 10;
		$grid->column("ID","id", 'width="20"');
		$grid->column("Name","name");
		$baseuri = "admin/rating/".$this->uri->segment(5);
		$link_edit = anchor("admin/rating/0/modify/<#id#>","Edit");
		$link_delete = anchor("admin/rating/0/delete/<#id#>","Delete");

		$grid->column("actions", "$link_edit | $link_delete", 'width="90"');
		$grid->add("admin/rating/create");
		$grid->build();

		//edit
		$edit = new DataEdit("Edit rating items", "rating_items");
		$edit->back_uri = $baseuri;
		$edit->back_save = true;
		$edit->back_delete = true;
		$edit->back_cancel_save = true;
		$edit->back_cancel_delete = true;

		$edit->title = new inputField("Name", "name");

		$edit->buttons("modify", "save", "undo", "delete");
		$edit->build();

		$data["crud"] = $grid->output . $edit->output;

		$data["rapyd_head"] = $this->rapyd->get_head();
		$this->load->view('admin', $data);
		
	}

	function logout(){
		$this->auth = $this->pear_auth->logout();
		redirect('admin/index', 'refresh');
	}

}
?>